/**
 * Created by Administrator on 2017/8/2.
 */
define(function(require, exports){
	var $ = require('jquery');
	var pubjs	= require('pubjs');
	var view  = require('@base/view');
	var util = require('util');
	
	// 可扩展的选择数据列表框
	var Base = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'M-selectAdver',
				'removeCastEvent': 'removeAdver', // 删除  广播事件
				'vModel': {
					'data': [],  //Username,name，Name
					'eventRemove': this.eventRemove
				},
				'cols':[
					{name:'_id',text:"ID", sort: true, render: 'center',align: "center", width: 60},
					{name:'UserName',text:LANG("名称"), type:'index', render: this.renderName, align: "left"},
					// {name:'name',text:LANG("名称"), type:'index', render: this.renderName, align: "left"}
					// {name:'Name',text:LANG("名称"), type:'index', render: this.renderName, align: "left"}
				] // 可覆盖
				,'url':'qualification/list'  //列表路径 ，可覆盖
			});
			
			this.Super('init', arguments);
		},
		afterBuild: function(){
			var self = this;
			var el = self.getDOM();
			// var c = self.getConfig();
			
			$([
				'<button class="btn btn-default selectBtn">' + LANG('选择') + '</button>',
				'<div class="selectCon">',
					'<div class="selectItems" v-for="item of data">',
						'<span class="valign-m" >',
						// '{{item._id || item.id}} | {{item.UserName||item.name}}',
						'{{item.UserName||item.name||item.Name}}',
						'</span>',
						'<i class="close icon-close font-danger valign-m" v-on:click="eventRemove(item)"></i>',
					'</div>',
				'</div>'
			].join('')).appendTo(el);
			self.vm.mount();
			self.uiBind(el.find('.selectBtn'), 'click', 'eventClickSelect');
		},
		eventRemove: function(item){
			var self = this;
			var c = self.getConfig();
			var removeItem = util.clone(item);
			self.vm.$.data.$remove(item);
			// 广播删除事件
			self.fire(c.removeCastEvent, {
				remove: removeItem,
				all: self.vm.get('data')
			});
		},
		eventClickSelect: function(){
			var self = this;
			var c = self.getConfig();
			var pop = self.get('pop');
			var ids = self.getIds();
			var cols = c.cols;
			if(!pop){
				self.createAsync('pop','modules/selectOnTab.pop', {
						'selectEvent': 'selectAdverSuccess',
						'width': 800,
						'getAllData': true,
						'main_config': {
							'url': c.url,
							'tab': {
								'all': {
									'text': LANG('全部'),
									// 'uri': 'modules/selectAdver.grid',
									'config': {
										'cols': cols
									}
								},
								'select': {
									'text': LANG('已选'),
									// 'uri': 'modules/selectAdver.grid',
									'config': {
										'cols': cols
									}
								}
							}
						}
					}, function(mod) {
						mod.reset().setData(ids).show().load();
					}
				);
			}else{
				pop.reset().setData(ids).show().load();
			}
			return false;
		},
		renderName: function(i, val, data){
			return val;
		},
		onSelectAdverSuccess: function(ev){
			this.setData(ev.param);
			return false;
		},
		getIds: function(){
			var ids = [];
			var data = this.vm.get('data');
			util.each(data, function(item){
				if(item){
					ids.push(item._id || item.id);
				}
			});
			return ids;
		},
		setData: function(data){
			var self = this;
			self.vm.set({
				data: util.clone(data)
			});
			return self;
		},
		getData: function(justIds){
			var self = this;
			var result = [];
			var data = self.vm.get('data');
			
			if(justIds){
				return self.getIds();
			}
			
			util.each(data, function(item){
				if(item){
					result.push({
						id: item._id || item.id,
						name: item.UserName || item.name
					});
				}
			});
			
			return result;
		},
		reset: function(){
			this.vm.reset();
			return this;
		}
	});
	exports.base = Base;
	
});